//
//  MaintainViewController.m
//  一键车讯
//
//  Created by Scott on 14/10/27.
//  Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//

#import "MaintainViewController.h"
#import "WebViewViewController.h"
#import "NetHandler.h"
#import "MainTain.h"
#import "MainTainCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

@interface MaintainViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *mainTableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, assign) int countx;
@end

@implementation MaintainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.arr = [NSMutableArray array];
    }
    return self;
}


// 数据
- (void)startData
{
    self.title = @"保养";
    NSString *str = @"http://mrobot.pcauto.com.cn/v2/cms/channels/5?pageNo=1&pageSize=20&v=4.0.0";
    [NetHandler networkWithURL:str completion:^(id result) {
        NSMutableArray *dicsArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in dicsArr) {
            MainTain *mainTain = [[MainTain alloc] initWithDictionary:dic];
            [self.arr addObject:mainTain];
            [mainTain release];
            
        }
        [self.mainTableView reloadData];
        [self setupRefresh];
    }];
}

// 视图
- (void)setupViews
{
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    self.mainTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height );
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
    [self.view addSubview:self.mainTableView];
    [_mainTableView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self startData];
    [self setupViews];
}


#pragma mark UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arr count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"CELL";
    MainTainCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[MainTainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    MainTain *mainTain = [self.arr objectAtIndex:indexPath.row];
    cell.titleLabel.text = mainTain.title;
    [cell.imgView setImageWithURL:[NSURL URLWithString:mainTain.Img]];
    return cell;
}

// 推出详情界面(WebView)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewViewController *webView = [[WebViewViewController alloc] init];
    MainTain *mainTain = [self.arr objectAtIndex:indexPath.row];
    webView.mainTain = mainTain;  // 2.将要传的值给第一步创建的属性
    
    [self.navigationController pushViewController:webView animated:YES];
    [webView release];
    
}



// 刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.mainTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.mainTableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.mainTableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.mainTableView footerEndRefreshing];
    //
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    //self.hotTableView.headerPullToRefreshText = @"准备刷新...";
    self.mainTableView.headerReleaseToRefreshText = @"松开即可刷新...";
    self.mainTableView.headerRefreshingText = @"正在刷新...";
    
    self.mainTableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.mainTableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.mainTableView.footerRefreshingText = @"加载中";
    
    self.countx = 2;
    NSLog(@"se.countx %d", self.countx);
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 刷新表格
        [self.mainTableView reloadData];
        
        [_mainTableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.mainTableView headerEndRefreshing];
    });
}

- (void)footerRereshing
{
    //_dataSource = [[NSMutableArray alloc]init];
    
    //    self.countx += 1;
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/5?pageNo=%d&pageSize=20&v=4.0.0", self.countx];
    [NetHandler networkWithURL:str completion:^(id result) {
        
        NSMutableArray *dicsArr = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in dicsArr) {
            MainTain *mai = [[MainTain alloc] initWithDictionary:dic];
            [self.arr addObject:mai];
        }
        [self.mainTableView reloadData];
        
    }];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.mainTableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.mainTableView footerEndRefreshing];
        _countx += 1;
        NSLog(@"self.countx %d", self.countx);
    });
    
}


- (void)dealloc
{
    [_mainTableView release];
    [_arr release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
